package com.hckjsec.system.controller;

import java.util.List;

import com.hckjsec.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.system.domain.SysFixedAssets;
import com.hckjsec.system.service.ISysFixedAssetsService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 固定资产Controller
 * 
 * @author cyt
 * @date 2021-10-12
 */
@RestController
@RequestMapping("/system/fixedAssets")
public class SysFixedAssetsController extends BaseController
{
    @Autowired
    private ISysFixedAssetsService sysFixedAssetsService;

    /**
     * 查询固定资产列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysFixedAssets sysFixedAssets)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            sysFixedAssets.setDeptId(SecurityUtils.getDeptId());
        }
        List<SysFixedAssets> list = sysFixedAssetsService.selectSysFixedAssetsList(sysFixedAssets);
        return getDataTable(list);
    }

    /**
     * 导出固定资产列表
     */
    @Log(title = "固定资产", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysFixedAssets sysFixedAssets)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            sysFixedAssets.setDeptId(SecurityUtils.getDeptId());
        }
        List<SysFixedAssets> list = sysFixedAssetsService.selectSysFixedAssetsList(sysFixedAssets);
        ExcelUtil<SysFixedAssets> util = new ExcelUtil<SysFixedAssets>(SysFixedAssets.class);
        return util.exportExcel(list, "assets");
    }

    /**
     * 获取固定资产详细信息
     */
    @GetMapping(value = "/{fixedId}")
    public AjaxResult getInfo(@PathVariable("fixedId") Long fixedId)
    {
        return AjaxResult.success(sysFixedAssetsService.selectSysFixedAssetsById(fixedId));
    }

    /**
     * 新增固定资产
     */
    @Log(title = "固定资产", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysFixedAssets sysFixedAssets)
    {
        sysFixedAssets.setDeptId(SecurityUtils.getDeptId());
        sysFixedAssets.setOperator(SecurityUtils.getUserId());
        return toAjax(sysFixedAssetsService.insertSysFixedAssets(sysFixedAssets));
    }

    /**
     * 修改固定资产
     */
    @Log(title = "固定资产", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysFixedAssets sysFixedAssets)
    {
        return toAjax(sysFixedAssetsService.updateSysFixedAssets(sysFixedAssets));
    }

    /**
     * 删除固定资产
     */
    @Log(title = "固定资产", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fixedIds}")
    public AjaxResult remove(@PathVariable Long[] fixedIds)
    {
        return toAjax(sysFixedAssetsService.deleteSysFixedAssetsByIds(fixedIds));
    }
}
